var AudioManager = require('geoAudioManager');

cc.Class({
    extends: cc.Component,

    properties: {
        musicSlide:{
            default:null,
            type: cc.Slider
        },
        sfxSlide:{
            default:null,
            type: cc.Slider
        },
        voiceSlide:{
            default:null,
            type: cc.Slider
        },
        closeButton: {
            default: null,
            type: cc.Button
        },
        showCloseButton: true,
    },

    // LIFE-CYCLE CALLBACKS:

    start () {
        this.closeButton.node.active = this.showCloseButton;
    },
    onEnable(){
        this.node.on('size-changed', this._updateSliders.bind(this));
    },
    onDisable(){
        this.node.off('size-changed', this._updateSliders.bind(this));
    },
    close(){
        this.node.active = false;
    },
    open(){
        this.node.active = true;
    },
    onSliderSFX(slider){
        cc.log("sfx: " + slider.progress);
        this._audio.setSFXVolumn(slider.progress);
    },
    onSliderMusic(slider){
        cc.log("music: " + slider.progress);
        this._audio.setMusicVolumn(slider.progress);

    },
    onSliderVoice(slider){
        cc.log("voice: " + slider.progress);
        this._audio.setVoiceVolumn(slider.progress);
    },
    updateSettings(){
        this._audio = AudioManager.Singleton;
        // 将目前的音量设置匹配好
        this.musicSlide.progress = this._audio.getMusicVolumn();
        this.sfxSlide.progress = this._audio.getSFXVolumn();
        this.voiceSlide.progress = this._audio.getVoiceVolumn();
    },
    _updateSliders(){
        var sliders = [this.musicSlide, this.voiceSlide, this.sfxSlide];
        sliders.forEach(e=>{
            e.getComponentInChildren(cc.Button).node.active = false;
        });
        this.scheduleOnce((()=>{
            sliders.forEach(e=>{
                e.getComponentInChildren(cc.Button).node.active = true;
            });

            this.updateSettings();
        }).bind(this), 0.1);
    }
    // update (dt) {},
});
